package com.lingshi.shopping_manager_api.controller;

import com.lingshi.shopping.entity.Goods;
import com.lingshi.shopping.result.BaseResult;
import com.lingshi.shopping.service.GoodsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 商品管理
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @DubboReference
    private GoodsService goodsService;

    /**
     * 新增商品
     * @param goods 商品对象
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Goods goods) {
        goodsService.add(goods);
        return BaseResult.success();
    }

    /**
     * 修改商品
     * @param goods 商品对象
     * @return
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Goods goods) {
        goodsService.update(goods);
        return BaseResult.success();
    }

    /**
     * 上下架
     * @param id
     * @param isMarketable
     * @return
     */
    @PutMapping("putAway")
    public BaseResult putAway(Long id, Boolean isMarketable) {
        goodsService.putAway(id, isMarketable);
        return BaseResult.success();
    }


    /**
     * 根据id查询商品信息
     * @param id 商品id
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Long id) {
        return BaseResult.success(goodsService.findById(id));
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @return 角色员分页对象
     */
    @GetMapping("/search")
    public BaseResult search(String goodsName,
                            @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        return BaseResult.success(goodsService.search(goodsName,page,size));
    }

}
